package amushen.module;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import amushen.base.BaseBO;
import amushen.base.DBUtil;

public class User extends BaseBO {

	public Object queryUser(Map paras,String user,InputStream is,DBUtil db){
		List list=db.query("User.queryUser", new Object[]{});
		if(list.size()<1){
			return "No Users.";
		}
		JSONArray arr=new JSONArray();
		JSONObject obj;
		Object[] row;
		try {
			for(int i=0;i<list.size();i++){
				obj=new JSONObject();
				row=(Object[])list.get(i);
				if(row==null)continue;
				obj.put("UserId", ((String)row[0]));			
				obj.put("UserName", (String)row[1]+"("+(String)row[2]+")");				
				arr.put(obj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return arr.toString();
	}
	
	public Object queryUserForComb(Map paras,String user,InputStream is,DBUtil db){
		List list=db.query("User.queryUser", new Object[]{});
		if(list.size()<1){
			return "No Users.";
		}
		JSONArray arr=new JSONArray();
		JSONObject obj;
		Object[] row;
		try {
			for(int i=0;i<list.size();i++){
				obj=new JSONObject();
				row=(Object[])list.get(i);
				if(row==null)continue;
				obj.put("data", (String)row[0]);			
				obj.put("label", (String)row[1]);
				arr.put(obj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return arr.toString();
	}
	
		
	
	public Object queryUserRole(Map paras,String user,InputStream is,DBUtil db){
		List list=db.query("User.queryUserRole", new Object[]{(String)paras.get("UserId")});
		if(list.size()<1){			
			list=db.query("User.queryAllRole", new Object[]{});
		}
		JSONArray arr=new JSONArray();
		JSONObject obj;
		Object[] row;
		try {
			for(int i=0;i<list.size();i++){
				obj=new JSONObject();
				row=(Object[])list.get(i);
				if(row==null)continue;
				obj.put("GroupId1", row[0]);			
				obj.put("UserId", row[1]);
				obj.put("GroupId2", row[2]);
				obj.put("GroupName", row[3]);
				arr.put(obj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return arr.toString();
	}
	
	
	public Object changeUserRole(Map paras,String user,InputStream is,DBUtil db){
		String para=(String)paras.get("req");
		if(para==null)return "No User";
		String[] ps=para.split(",");
		if(ps.length<2)return "No Role";
		int ret=-1;
		String UserId=ps[0];
		ret=db.update("User.deleteUserRole", new Object[]{UserId});
		if(ret==-1){
			db.rollback();
			return "Can't delete User Role";
		}
		for(int i=1;i<ps.length;i++){
			ret=db.update("User.insertUserRole", new Object[]{(String)ps[i],UserId});
			if(ret==-1){
				db.rollback();
				return "Fail to insert User Role";
			}
		}
		db.commit();
		return "Success";
	}
	
	public Object deleteUser(Map paras,String user,InputStream is,DBUtil db){
		String userId=(String)paras.get("UserId");
		int ret=-1;
		ret=db.update("User.deleteUserRole",new Object[]{userId});
		if(ret==-1){
			db.rollback();
			return "Fail delete User Role";
		}
		ret=db.update("User.deleteUser",new Object[]{userId});
		if(ret==-1){
			db.rollback();
			return "fail delete User";
		}
		db.commit();
		return "Success";
	}
	
	public Object insertUser(Map paras,String user,InputStream is,DBUtil db){
		String userName=(String)paras.get("username");
		String dept=(String)paras.get("dept");
		try {
			userName=java.net.URLDecoder.decode(userName,"UTF-8");
			dept=java.net.URLDecoder.decode(dept,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}	
		
		String password=(String)paras.get("password");
//		String userId="1";
//		List list=db.query("User.queryUserId", new Object[]{});
//		if(list.size()>0){
//			Object[] row=(Object[])list.get(0);
//			int temp=Integer.parseInt(((String)row[0]).trim());
//			temp++;
//			userId=temp+"";
//		}
		int ret=db.update("User.insertUser", new Object[]{userName,password,dept});
		if(ret==-1){
			db.rollback();
			return "failed to insert user";
		}
		db.commit();
		return "Success";
	}
}
